package world.snowcrystal.nova.config;

import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

@Configuration
public class SchedulerServiceConfig {
    @Bean
    public Scheduler schedulerService() {
        ThreadFactory threadFactory = new ThreadFactory() {
            private final AtomicLong counter = new AtomicLong(0L);

            @Override
            public Thread newThread(@NotNull Runnable r) {
                Thread thread = new Thread(r, "FF-Thread-Pool-" + counter.getAndIncrement());
                thread.setDaemon(false);
                return thread;
            }
        };

        return Schedulers.from(Executors.newScheduledThreadPool(10, threadFactory));
    }
}
